package com.henu.art.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.henu.art.entity.Research;
import com.henu.art.service.AuthService;
import com.henu.art.service.ResearchService;
import com.henu.art.util.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("/research")
@Tag(name = "科研成果管理", description = "科研成果相关操作接口")
public class ResearchController {
    @Resource
    private ResearchService researchService;
    @Resource
    private AuthService authService;

    @GetMapping("/list")
    @Operation(summary = "获取科研成果列表", description = "分页获取科研成果信息")
    public Result<IPage<Research>> getResearchList(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        return Result.ok(researchService.getResearchPage(pageNum, pageSize));
    }

    @GetMapping("/listById/{id}")
    @Operation(summary = "根据工号获取科研成果列表", description = "分页获取指定工号的科研成果信息")
    public Result<IPage<Research>> getResearchListById(
            @PathVariable String id,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        return Result.ok(researchService.getResearchPageById(id, pageNum, pageSize));
    }

    @PostMapping("/add")
    @Operation(summary = "新增科研成果", description = "添加一个新的科研成果")
    public Result<?> addResearch(@RequestBody Research research, @RequestHeader("token") String token) {
        String userId = authService.getUsernameByToken(token);
        research.setId(Long.valueOf(userId));
        return researchService.save(research) ? Result.ok("添加成功") : Result.error("添加失败");
    }

    @PostMapping("/update")
    @Operation(summary = "修改科研成果", description = "修改科研成果信息")
    public Result<?> updateResearch(@RequestBody Research research) {
        return researchService.updateById(research) ? Result.ok("修改成功") : Result.error("修改失败");
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除科研成果", description = "根据ID删除科研成果")
    public Result<?> deleteResearch(@RequestParam Long id) {
        return researchService.deleteResearch(id);
    }
}